package main

import (
	"flag"
	"log"
	"net/http"
	"strings"
)

var (
	listen = flag.String("listen", ":12000", "listen address")
	dir    = flag.String("dir", ".", "directory to serve")
)

func main() {
	flag.Parse()
	log.Printf("listening on %q %q...", *listen, *dir)
	fileServer := http.FileServer(http.Dir(*dir));
	http.Handle("/", wasmFileServer(fileServer))
	err := http.ListenAndServe(*listen, fileServer)
	log.Fatalln(err)
}

func wasmFileServer(fileServer http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasSuffix(r.URL.Path, ".wasm") {
			w.Header().Set("Content-Type", "application/wasm")
		}
		fileServer.ServeHTTP(w, r)
	})
}